package com.example.user.userUtil;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @author 贾明祯
 * @ClassName TestEasyExal
 * @Date 2020/10/22 16:01
 * @Introduce
 */
public class TestEasyExal {
    public static void main(String[] args) {
        // 创建表头
        String[] title = {"id","name","sex"};
        //创建Excel工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个工作表sheet
        HSSFSheet sheet = workbook.createSheet();
        //创建第一行
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        // 插入第一行
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i+1);
            cell.setCellValue(title[i]);
        }
        // 追加数据
        for (int i = 1; i < 10; i++) {// 这里的int 起始是1 也就是第二行开始
            HSSFRow nexTrow = sheet.createRow(i);
            HSSFCell cell2 = nexTrow.createCell(0);
            cell2.setCellValue("a"+i);
            cell2 = nexTrow.createCell(1);
            cell2.setCellValue("user"+i);
            cell2 = nexTrow.createCell(2);
            cell2.setCellValue("男"+i);
        }
        // 创建一个文件
        File file = new File("D:/poi.xls");
        try {
            file.createNewFile();
            // 将内容存盘
            FileOutputStream stream = FileUtils.openOutputStream(file);
            workbook.write(stream);
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
